package com.ljkj.cordial.chat.widget;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 倒计时
 */
public class DownTimeTextView extends AppCompatTextView implements View.OnClickListener, Runnable {

    private int maxDownTime = 60;
    private int currentPosition;
    private ScheduledExecutorService exec;

    public void setMaxDownTime(int maxDownTime) {
        this.maxDownTime = maxDownTime;
    }

    public DownTimeTextView(Context context) {
        super(context);
        init();
    }

    public DownTimeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DownTimeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        this.currentPosition = maxDownTime;
        setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bg_black_stroke_angle));
        setText("    获取验证码    ");
        setTextColor(ContextCompat.getColor(getContext(), R.color.black3));
        setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
        setGravity(Gravity.CENTER);
//        int d16 = TypedValue.complexToDimensionPixelOffset(16, getResources().getDisplayMetrics());
//        int d8 = TypedValue.complexToDimensionPixelOffset(8, getResources().getDisplayMetrics());
//        setPadding(d16, d8, d16, d8);
        setOnClickListener(this);


    }

    public void reset() {
        if (exec != null) {
            exec.shutdownNow();
        }
        this.currentPosition = maxDownTime;
        setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bg_black_stroke_angle));
        setText("    获取验证码    ");
        setTextColor(ContextCompat.getColor(getContext(), R.color.black3));
        setEnabled(true);
    }


    private void refreshCaptcha(TextView tvCaptcha, int time) {
        if (((Activity) getContext()).isFinishing()) {
            return;
        }
        if (time == 0) {
            tvCaptcha.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bg_black_stroke_angle));
            tvCaptcha.setTextColor(ContextCompat.getColor(getContext(), R.color.black3));
            tvCaptcha.setEnabled(true);
            tvCaptcha.setText("    获取验证码    ");
        } else {
            tvCaptcha.setText("    " + time + "s    ");
        }
    }

    private void getCaptcha(TextView tvCaptcha) {
        tvCaptcha.setEnabled(false);
        tvCaptcha.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.bt_gray_circle_angle));
        tvCaptcha.setTextColor(ContextCompat.getColor(getContext(), R.color.gray_500));

    }

    @Override
    public void onClick(View v) {
        if (onCaptchaListener != null) {
            boolean flg = onCaptchaListener.onClick(v);
            if (flg) {
                getCaptcha(this);
                if (exec == null || exec.isShutdown()) {
                    exec = Executors.newSingleThreadScheduledExecutor();
                }
                exec.scheduleAtFixedRate(this, 0, 1, TimeUnit.SECONDS);
            }
        }
    }

    private OnCaptchaListener onCaptchaListener;

    public void setOnCaptchaListener(OnCaptchaListener onCaptchaListener) {
        this.onCaptchaListener = onCaptchaListener;
    }

    @Override
    public void run() {
        if (((Activity) getContext()).isFinishing()) {
            if (exec != null) {
                exec.shutdownNow();
            }
            return;
        }

        if (currentPosition >= 0) {
            ((Activity) getContext()).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    refreshCaptcha(DownTimeTextView.this, currentPosition);
                }
            });
            if (currentPosition == 0) {
                exec.shutdownNow();
                currentPosition = maxDownTime;
                return;
            }
        }
        currentPosition--;
    }

    public interface OnCaptchaListener {
        boolean onClick(View v);
    }

}
